import random
import time

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait


def open_browser( browser="chrome" ):
    # 打开浏览器
    driver = None
    if browser == "chrome":
        driver = webdriver.Chrome()
    elif browser == "firefox":
        driver = webdriver.Firefox()
    elif browser == "ie":
        driver = webdriver.Ie()
    else:
        print("请输入正确的浏览器,例如'chrome','firefox','ie'")
    return driver


class Base(object):

    def __init__( self, driver ):
        self.driver = driver

    def open_url( self, url ):
        """
        打开网址
        :return:
        """
        self.driver.get(url)
        # 窗口最大化
        self.driver.maximize_window()

    def find_element( self, locator, timeout=10 ):
        """定位一个元素"""
        element = WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
        return element

    def find_elements( self, locator, timeout=10 ):
        """定位一组元素"""
        elements = WebDriverWait(self.driver, timeout).until(EC.presence_of_all_elements_located(locator))
        return elements

    def select_by_value( self, locator, value, timeout=10 ):

        """
        value值定位下拉框选项
        :param locator: 定位器,是一个元组
        :param value: value值的接收
        :param timeout: 最大等待时间
        :return:
        """
        element = self.find_element(locator, timeout)
        select_value = Select(element)
        select_value.select_by_value(value)

    def select_by_index( self, locator, index, timeout=10 ):
        """
        索引定位下拉框选项
        :param locator: 定位器,是一个元组
        :param index: 接收索引
        :param timeout: 最大等待时间
        :return:
        """
        element = self.find_element(locator, timeout)
        select_index = Select(element)
        select_index.select_by_index(index)

    def select_by_text( self, locator, text, timeout=10 ):
        """
        文本信息定位下拉框选项
        :param locator: 定位器,是一个元组
        :param text: 接收文本信息
        :param timeout: 最大等待时间
        :return:
        """
        element = self.find_element(locator, timeout)
        select_text = Select(element)
        select_text.select_by_visible_text(text)

    def radio( self, locator, timeout=10 ):
        """
        单选框选中定位
        :param locator: 定位器,是一个元组
        :param timeout: 最大等待时间
        :return:
        """
        element = self.find_element(locator, timeout)
        if element.is_selected():
            print(f"已被选中")
        else:
            element.click()

    def checkbox( self, locator, timeout=10 ):
        """
        选择单个复选框
        :param locator: 定位器,是一个元组
        :param timeout: 定位器,是一个元组
        :return:
        """
        element = self.find_element(locator, timeout)
        if element.is_selected():
            print(f"已勾选")
        else:
            element.click()

    def checkboxes( self, locator, timeout=10 ):
        """
        全选复选框
        :param locator: 定位器,是一个元组
        :param timeout: 最大等待时间
        :return:
        """
        elements = self.find_elements(locator, timeout)
        for element in elements:
            if element.is_selected():
                print(f"已勾选")
            else:
                element.click()

    def click( self, locator, timeout=10 ):
        """
        点击元素
        :return:
        """
        element = self.find_element(locator, timeout)
        element.click()

    def send_keys( self, locator, text, timeout=10 ):
        """
        写入数据
        :return:
        """
        element = self.find_element(locator, timeout)
        element.clear()
        element.send_keys(text)

    def is_text_in_element( self, locator, text, timeout=10 ):
        """
        判断文本是否存在于文本中
        :return:
        """
        try:
            result = WebDriverWait(self.driver, timeout=timeout).until(EC.text_to_be_present_in_element(locator, text))
            return result
        except:
            return False

    def is_value_in_element( self, locator, value, timeout=10 ):
        """
        判断value属性值是否相同
        :return:
        """
        try:
            result = WebDriverWait(self.driver, timeout).until(
                EC.text_to_be_present_in_element_value(locator, value))
            return result
        except:
            return False

    def back( self ):
        """返回上一页"""
        self.driver.back()

    def random_choice_select( self, loc_1, loc_2="option" ):
        """随机选择下拉框"""
        select = self.find_element(loc_1)
        province_list = select.find_elements_by_css_selector(loc_2)
        num = random.randint(1, len(province_list) - 1)
        Select(select).select_by_index(num)

    def accept( self ):
        """弹出提示框操作"""
        alert = self.driver.switch_to.alert  # 进入弹框
        alert.accept()  # 点击弹框中的确定按钮

    def close_browser( self ):
        """
        关闭浏览器
        :return:
        """
        self.driver.quit()


if __name__ == '__main__':
    # 调用方法打开浏览器
    driver = open_browser()
    # 实例化对象
    base = Base(driver)
    # 打开网站
    url = "http://localhost:8080/ecshop/user.php"
    base.open_url(url)
    time.sleep(2)
    # 输入用户名
    locator = ("name", "username")
    base.send_keys(locator=locator, text="yangjun")
    time.sleep(2)
    # 输入密码
    locator = ("name", "password")
    base.send_keys(locator=locator, text="123456")
    time.sleep(2)
    # 点击立即登录
    locator = ("name", "submit")
    base.click(locator=locator)
    time.sleep(2)
    # 关闭
    base.close_browser()
